/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import javax.swing.JOptionPane;

/**
 * Clase JFrameCategoria encargada de crear las categorias. 
 * @author Victoria Fuenmayor y José Gabriel Rivera
 */
public class JFrameCategoria extends javax.swing.JFrame {
    
    private GUI gui;

    /**
     * Recibe por parámetro la clase GUI (Gráfica) e inicializa los componentes.
     * @param gui 
     */  
    public JFrameCategoria(GUI gui) {
        initComponents();
        this.setLocation(280, 250);
        this.gui = gui;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelInstruccion = new javax.swing.JLabel();
        jTextFieldNombreCat = new javax.swing.JTextField();
        jButtonCrear = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setTitle("Añadir categoria");
        setResizable(false);

        jLabelInstruccion.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelInstruccion.setText("Ingrese el nombre de la categoria:");

        jTextFieldNombreCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNombreCatActionPerformed(evt);
            }
        });

        jButtonCrear.setText("Crear");
        jButtonCrear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCrearMouseClicked(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabelInstruccion, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                        .addComponent(jTextFieldNombreCat))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonCrear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelInstruccion, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldNombreCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCrear)
                    .addComponent(jButtonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldNombreCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNombreCatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldNombreCatActionPerformed

    /**
     * MouseClicked del boton cancelar, si es presionado se oculta la ventana.
     * @param evt 
     */
    private void jButtonCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelarMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButtonCancelarMouseClicked

    /**
     * Mouse Clicked para el botón de crear categoría.
     * Notifica al usuario en caso de sobrepasar el límite del nombre, 
     * asi como tambien si existen categorías del mismo tipo.
     * Crea la categoría en el primer espacio libre que encuentra con su nombre respectivo.
     * Y carga la lista de categorias.
     * 
     * @param evt 
     */   
    private void jButtonCrearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCrearMouseClicked
        // TODO add your handling code here:        
        if (jTextFieldNombreCat.getText().length() > 25){
            JOptionPane.showMessageDialog(this, "Error: Nombre sólo permite 25 caractéres.");
        }else if (gui.getCategoria().buscarObjeto(jTextFieldNombreCat.getText())!=null){   
            JOptionPane.showMessageDialog(this, "Error: Existe una categoría con ese nombre.");
        }else{
            logic.Categoria cat = new logic.Categoria(jTextFieldNombreCat.getText(), this.gui.getCategoria().firstFree());
            this.gui.getCategoria().addObject(cat, cat.getId());
            this.gui.getListaCat().CargarCat();
            this.setVisible(false);
        }
    }//GEN-LAST:event_jButtonCrearMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameCategoria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameCategoria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameCategoria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameCategoria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameCategoria(new GUI("Raffle System")).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonCrear;
    private javax.swing.JLabel jLabelInstruccion;
    private javax.swing.JTextField jTextFieldNombreCat;
    // End of variables declaration//GEN-END:variables
}
